#include "gtest/gtest.h"

#include <iostream>

#include "SimpleSequenceReader.h"
#include "Topic.h"
#include "recovery/Block.h"

using recovery::Block;


static void printHex(const unsigned char * buf, const size_t bytes){
    std::cout << std::string(reinterpret_cast<const char *>(buf), bytes) << std::endl;
	return ;
}
TEST(SequenceReaderTest, testMain){
    auto tp = std::make_unique<Topic>("stream", ".");
	{
		auto seqr = std::make_unique<SimpleSequenceReader>(*tp);
		ASSERT_TRUE(seqr->open());
		auto block = std::make_unique<Block>();
		while(true){
            int ret = seqr->polling(200);
			if(ret > 0){ // readable
				size_t readBytes = 0;
				auto s = seqr->read(block->addr(), block->bytes(), readBytes);
				if(s){
                    std::cout << "read:" <<readBytes <<" bytes, :" << s.what() << std::endl;
					printHex(block->addr(), readBytes);
				}
				else{
					std::cerr << "read error" << std::endl;	
					break;
				}
			}
            else if(ret == 0){//timeout
                static int i = 0;
                if(i++ >= 3) break;
            }
            else{
                std::cerr << "polling got an error" << std::endl;
                break;
            }
		}
		ASSERT_TRUE(seqr->close());
	}
}
